<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true" :model="params">
        <el-form-item label="生辰列表">
          <el-input v-model="searchData.name" size="small" placeholder="名字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading" 
      :data="tableData.data"
      @sort-change="sort"
      border
      style="width: 100%"
      max-height="100%">
      <el-table-column
        prop="id"
        width="80"
        sortable
        label="ID">
      </el-table-column>
      <el-table-column
        prop="name"
        sortable
        label="名字">
      </el-table-column>
      <el-table-column
        prop="sex"
        sortable
        label="性别">
        <template slot-scope="scope">
            <span v-if="scope.row.sex === 1">男</span>
            <span v-if="scope.row.sex === 2">女</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="birth_address"
        sortable
        label="出生地">
      </el-table-column>
      <el-table-column
        prop="lunar_calendar"
        sortable
        label="阴历">
      </el-table-column>
      <el-table-column
        prop="gregorian_calendar"
        sortable
        label="阳历">
      </el-table-column>
      <el-table-column
        fixed="right"
        width="180"
        label="操作">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editRow(scope.row)"
            type="default"
            size="mini">
            分析
          </el-button>
          <el-button
            @click.native.prevent="show(scope.row)"
            type="default"
            size="mini">
            结果
          </el-button>
          <!-- <el-button
            @click.native.prevent="leadRow(scope.row)"
            type="default"
            size="mini">
            导语
          </el-button> -->
          <!-- <el-button
            @click.native.prevent="deleteRow(scope.row)"
            type="danger"
            size="mini">
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="block toolbar">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.current_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="parseInt(tableData.per_page)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
    <!-- dialog -->
    <el-dialog class="dialog" title="分类" :visible.sync="visible">
      <el-form :model="formData" ref="formData" :rules="formRules"  label-width="120px">
       
        <el-form-item label="工作打分" prop="work_fraction">
           <el-input-number v-model="formData.work_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="创新打分" prop="innovate_fraction">
           <el-input-number v-model="formData.innovate_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="稳定打分" prop="stabilize_fraction">
           <el-input-number v-model="formData.stabilize_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="婚姻打分" prop="marriage_fraction">
           <el-input-number v-model="formData.marriage_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="健康打分" prop="fitness_fraction">
           <el-input-number v-model="formData.fitness_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="收入打分" prop="income_fraction">
           <el-input-number v-model="formData.income_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="能力打分" prop="capacity_fraction">
           <el-input-number v-model="formData.capacity_fraction" :min="1" :max="5" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="工作导语" prop="type">
        <el-select v-model.number="work_proposal" placeholder="请选择工作建议">
          <el-option
            v-for="item in categories"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
         <el-select
          v-model="formData.work_proposal"
          filterable
          placeholder="请选择">
          <el-option
            v-for="item in optFor"
            :key="item.id"
            :label="item.introduction"
            :value="item.introduction">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创新导语" prop="type">
      <el-select v-model.number="innovate_proposal" placeholder="请选择创新建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.innovate_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="稳定导语" prop="type">
      <el-select v-model.number="stabilize_proposal" placeholder="请选择稳定建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.stabilize_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="婚姻导语" prop="type">
      <el-select v-model.number="marriage_proposal" placeholder="请选择婚姻建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.marriage_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="健康导语" prop="type">
      <el-select v-model.number="fitness_proposal" placeholder="请选择健康建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.fitness_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
     <el-form-item label="收入导语" prop="type">
      <el-select v-model.number="income_proposal" placeholder="请选择收入建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.income_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="能力导语" prop="type">
      <el-select v-model.number="capacity_proposal" placeholder="请选择能力建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.capacity_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
      <el-form-item label="其他导语" prop="type">
      <el-select v-model.number="else_proposal" placeholder="请选择其他建议">
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
        <el-select
        v-model="formData.else_proposal"
        filterable
        placeholder="请选择">
        <el-option
          v-for="item in optFor"
          :key="item.id"
          :label="item.introduction"
          :value="item.introduction">
        </el-option>
      </el-select>
    </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false" :disabled="editLoading">取 消</el-button>
        <el-button type="primary" :loading="editLoading" @click="handleCreateOrEdit('formData')">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
  import {generateUrl} from '@/utils/helpers'
  export default {
    data () {
      return {
        loading: false,
        tableData: {
          first_page_url: '',
          from: '',
          to: '',
          last_page: '',
          last_page_url: '',
          next_page_url: '',
          prev_page_url: '',
          path: '',
          per_page: 0,
          total: 0,
          data: []
        },
        categories: {},
        searchData: {
          name: ''
        },
        params: {
          order_by: 'id',
          order_type: 'desc',
          page_size: 20,
          page: 1
        },
        // dialog
        editLoading: false,
        visible: false,
        formData: {
          id: null
        },
        formRules: {},
        optFor: {},
        work_proposal: {},
        innovate_proposal: {},
        stabilize_proposal: {},
        marriage_proposal: {},
        fitness_proposal: {},
        income_proposal: {},
        capacity_proposal: {},
        else_proposal: {},
        typeid: {}
      }
    },
    mounted () {
      this._getData()
      this._getCategories()
      console.log(this.formData.type)
    },
    watch: {
      params: {
        deep: true,
        handler (newValue) {
          this._getData()
        }
      },
      work_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      innovate_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      stabilize_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      marriage_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      fitness_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      income_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      capacity_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      },
      else_proposal: {
        deep: true,
        handler (newValue) {
          this._gettype(newValue)
        }
      }
    },
    methods: {
      show (item) {
        this.$router.push('/distribution/' + item.id)
      },
      deleteRow (row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http.delete('/yiJing/' + row.id).then((response) => {
            this.loading = false
            this.$message.success('删除成功')
            this._getData()
          }).catch((error) => {
            this.loading = false
            console.log(error)
          })
        })
      },
      editRow (item) {
        this.formData = Object.assign({}, item)
        this.visible = true
      },
      onSubmit () {
        this.params.name = this.searchData.name
      },
      handleSizeChange (val) {
        this.params.page_size = val
      },
      handleCurrentChange (val) {
        this.params.page = val
      },
      sort (data) {
        let orderType = ''
        if (data.order === 'ascending') {
          orderType = 'asc'
        }
        if (data.order === 'descending') {
          orderType = 'desc'
        }
        this.params.order_type = orderType
        this.params.order_by = data.prop
      },
      create () {
        this._restFormData()
        this.visible = true
      },

      handleCreateOrEdit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http({
              method: this.formData.id === null ? 'post' : 'put',
              url: this.formData.id === null ? '/yiJing' : '/yiJing/' + this.formData.id,
              data: this.formData
            }).then((response) => {
              this.$message.success(response.data.message)
              this.editLoading = false
              this.visible = false
              this._getData()
            }).catch((error) => {
              this.editLoading = false
              console.log(error)
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      _restFormData () {
        this.formData = {
          id: null,
          name: '',
          display_name: '',
          description: ''
        }
      },
      _getCategories () {
        this.$http.get('/yiJingIntroductionSort').then((response) => {
          this.categories = response.data
        }).catch((error) => {
          console.log(error)
        })
      },
      _gettype (id) {
        this.$http.get('/YiJingIntroduction?type=' + id).then((response) => {
          this.optFor = response.data.data
          console.log(this.optFor)
        }).catch((error) => {
          console.log(error)
        })
      },
      _getData () {
        let url = generateUrl('/yiJing', this.params)
        this.loading = true
        this.$http.get(url).then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog {
    .el-select {
      width: 80%;
    }
    .el-input {
      width: 80%;
    }
    .el-textarea {
      width: 80%;
    }
  } 
</style>
<style>
.el-form-item__content{
  display: flex;
}
</style>

